﻿
using AvaDemo.Models;
using AvaDemo.Services;
using AvaDemo.Views;
using AvaDemo.Views.Dialogs;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DynamicData;
using FreeSql;
using Material.Icons;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Win32;
using SkiaSharp;
using SukiUI.Controls;
using SukiUI.MessageBox;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using static FreeSql.Internal.GlobalFilter;


namespace AvaDemo.ViewModels
{

    public partial class MainViewModel : ObservableObject
    {
        public DBContext dBContext;

        private ObservableCollection<PerInfo> _perInfoDatas;

        public ObservableCollection<PerInfo> PerInfoDatas
        {
            get => _perInfoDatas;
            set => SetProperty(ref _perInfoDatas, value);
        }
        public MainViewModel()
        {
            _isShow = "Collapsed";
            SelectClick();
        }


        [ObservableProperty] private string _isShow;

    
        /// <summary>
        /// 查询
        /// </summary>
        [RelayCommand]
        public async void SelectClick()
        {
            PerInfoDatas = new ObservableCollection<PerInfo>();
            dBContext = new DBContext();
            var da = await dBContext.GetAllData();

            if(da != null)
            {
                PerInfoDatas = new ObservableCollection<PerInfo>(da);
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        [RelayCommand]
        public async void AddClick()
        {
            PerInfoDatas.Add(new PerInfo()
            {
                Age="     ", CellPhoneNumber="     ", City="      ", Id=Guid.NewGuid(), Identity="     ", IdentityCard="     ", IsSelect=false, Name="        "
            });

            
        }

        /// <summary>
        /// 保存
        /// </summary>
        [RelayCommand]
        public async void SaveClick()
        {
            var updateList = PerInfoDatas.Where(x => x.IsSelect);
            using (var db = dBContext.GetDb())
            {
                var oldPer =await  db.Select<PerInfo>().ToListAsync(x=>x.Id);
                var upDatelis = new List<PerInfo>();
                var addlis = new List<PerInfo>();

                foreach (var item in updateList)
                {
                    if (oldPer.Contains(item.Id)) 
                    {
                        upDatelis.Add(item);
                    }
                    else
                    {
                        addlis.Add(item);
                    }
                }
                if(upDatelis.Count > 0)
                {
                    db.Update<PerInfo>().SetSource(upDatelis).ExecuteAffrows();
                }
                if (addlis.Count > 0)
                {
                    db.Insert<PerInfo>(addlis).ExecuteAffrows();

                }
            }
            

            SelectClick();
        }

        /// <summary>
        /// 删除
        /// </summary>
        [RelayCommand]
        public void DeleteClick()
        {
            var isSelete = PerInfoDatas.Where(x => x.IsSelect).Select(x => x.Id);

            if (isSelete.Any())
            {
                using (var db = dBContext.GetDb())
                {
                    var t = db.Delete<PerInfo>().Where(x => isSelete.Contains(x.Id)).ExecuteAffrows(); ;
                }
            }

            SelectClick();
        }


        /// <summary>
        /// 是否显示
        /// </summary>
        [RelayCommand]
        public void IsShowClick()
        {
            if (PerInfoDatas.Where(x => x.IsSelect).Count() > 0)
            {
                
                _isShow = "Collapsed";
            }
            else
            {
                _isShow = "Visible";
            }
        }
    }
}


    

